package tul.cot.job.output;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.io.filefilter.RegexFileFilter;

public class LocalGetter extends OutputGetter {

	private static final FileFilter FILTER = new RegexFileFilter("part-r-.*");
	
	public LocalGetter() {
		super(null);
	}
	
	@Override
	protected Iterable<String> getLines() throws IOException {
		List<String> lines = new ArrayList<String>();
		
		File fromDir = getFromDir();
		
		for (File f: fromDir.listFiles(FILTER)) {
			BufferedReader reader = new BufferedReader(new FileReader(f));
			String line = null;
			while ((line = reader.readLine()) != null) {
				lines.add(line);
			}
		}
		
		Collections.sort(lines, getLabelComparator());
		return lines;
	}

}
